function getAdmin(plr,level)
	if level then
		local result=exports["ogrpg-db"]:dbGet("SELECT * from ogrpg_admins WHERE serial=? AND level=?", getPlayerSerial(plr), level)
		if result and #result > 0 then
			return true
		else
			return false
		end
	else
		local result=exports["ogrpg-db"]:dbGet("SELECT * from ogrpg_admins WHERE serial=?", getPlayerSerial(plr))
		if result and #result > 0 then
			return true
		else
			return false
		end
	end
end

function getLevel(plr)
	if plr then
		local result=exports["ogrpg-db"]:dbGet("SELECT level from ogrpg_admins WHERE serial=? limit 1", getPlayerSerial(plr))
		if result and #result > 0 then
			if tonumber(result[1].level) > 0 then
			return result[1].level
			else
			return false
			end
		else
			return false
		end
	end
end



rangi = {
[5] = "#a31303Manager",
[4] = "#cc0000RCON",
[3] = "#ff0000Administrator",
[2] = "#006699Moderator",
[1] = "#00CC00Supporter",
}

function wyswietl(plr,tabela)
	if not plr then return end
	if type(tabela) ~= "table" then outputChatBox("* Aktualnie nie ma administracji na duty",plr,255,0,0) return end
	outputChatBox(" ✪ Dostępna Administracja ✪ ",plr,255,0,0)
	for _,s in pairs(tabela) do
		outputChatBox(getPlayerName(s[1]):gsub("#%x%x%x%x%x%x","").."#ff0000 ["..getElementData(s[1],"id").."".."]  #ccc000"..s[2]:gsub("#%x%x%x%x%x%x","").."",plr,255,255,255,true)
	end
end

function cmd_admins(plr)
   local tabela = {}
   local ilosc = 0
	for i,v in ipairs(getElementsByType("player")) do
		if not getElementData(v,"p:inv") then 
		if getAdmin(v) then
			if getElementData(v,"player:admin") then
			local ranga = rangi[tonumber(getLevel(v))]
			tabela[i] = {v,ranga}
			ilosc = i
		 end
		 end
		 end
	end
	if ilosc < 1 then tabela = false end
	wyswietl(plr,tabela)
end


addCommandHandler("admins", cmd_admins, false, false)
addCommandHandler("ekipa", cmd_admins, false, false)

addEvent("admin:logs", true)
addEventHandler("admin:logs", root, function(text)
local query=exports["ogrpg-db"]:dbSet("INSERT INTO ogrpg_logs (name,data) VALUES (?,NOW())", text)
end)


addCommandHandler("ucho", function(plr,cmd)
if getAdmin(plr,5) or getAdmin(plr,4) or getAdmin(plr,3) or getAdmin(plr,2) or getAdmin(plr,1) then
	local x = getElementData(plr,"player:ucho")
	setElementData(plr,"player:ucho",not x)
	end
end)

addCommandHandler("duty", function(plr,cmd)
	local pobierz = getElementData(plr,"player:uid")
	local result=exports["ogrpg-db"]:dbGet("SELECT * from ogrpg_admins WHERE serial=?", getPlayerSerial(plr))
	if result and #result > 0 then
	if pobierz == result[1].uid then
	if getAdmin(plr,5) or getAdmin(plr,4) or getAdmin(plr,3) or getAdmin(plr,2) or getAdmin(plr,1) then
	if getElementData(plr,"gracz:mute") then outputChatBox("#BC8F8F[DUTY]#FFFFFF Nie możesz wejść na duty, ponieważ posiadasz wyciszenie na czat.", plr, 255, 0, 0,true) return end
		if not getElementData(plr,"player:admin") then
			giveWeapon(plr,22)
			toggleControl(plr, "fire", false)
			toggleControl(plr, "action", true)
			toggleControl(plr, "aim_weapon", true)
			if getAdmin(plr, 1) then
			setElementData(plr,"player:level",1)
			elseif getAdmin(plr, 2) then
			setElementData(plr,"player:level",2)
			elseif getAdmin(plr, 3) then
			setElementData(plr,"player:level",3)
			elseif getAdmin(plr, 4) then
			setElementData(plr,"player:level",4)
			elseif getAdmin(plr, 5) then
			setElementData(plr,"player:level",5)
			end
			setElementData(plr,"player:admin",true)
			exports["ogrpg-db"]:dbSet("INSERT INTO pssm_duty_logs (nick,operation) VALUES (?,?)", ""..getPlayerName(plr):gsub("#%x%x%x%x%x%x","").." [UID:"..getElementData(plr,"player:uid").."]", "Wejście na służbę.")
			outputChatBox("#BC8F8F[DUTY]#FFFFFF Zalogowałeś(aś) się na duty.", plr, 255, 0, 0,true)
		else
			takeWeapon(plr,22)
			toggleControl(plr, "fire", false)
			toggleControl(plr, "action", false)
			toggleControl(plr, "aim_weapon", false)
			removeElementData(plr,"player:admin")
			removeElementData(plr,"player:level")
			exports["ogrpg-db"]:dbSet("INSERT INTO pssm_duty_logs (nick,operation) VALUES (?,?)", ""..getPlayerName(plr):gsub("#%x%x%x%x%x%x","").." [UID:"..getElementData(plr,"player:uid").."]", "Zejście ze służby.")
			outputChatBox("#BC8F8F[DUTY]#FFFFFF Wylogowałes(aś) się z duty.", plr, 255, 0, 0,true)
		end
	    end
	else
	end
	end
end)

scianarapsow = createObject ( 16637,-1931.6,883.27,35.41 )
setObjectScale(scianarapsow,3)
reportView = {{"#FF8C00Raporty:",0}}
setElementData(scianarapsow,"sciana:raportow",reportView)

addEvent("admin:addReport", true)
addEventHandler("admin:addReport", root, function(text,id)
	table.insert(reportView, {text,id})
	if #reportView > 15 then
		table.remove(reportView, 2)
	end
	setElementData(scianarapsow,"sciana:raportow",reportView)
end)

addEvent("admin:removeReport", true)
addEventHandler("admin:removeReport", root, function(id)
	for i=#reportView, 2, -1 do
		if reportView[i][2] == id then
			table.remove(reportView,i)
		end
	end
	setElementData(scianarapsow,"sciana:raportow",reportView)
end)

scianatext = createObject ( 981,0,0,0 )
textView = {{"#FF8C00Logi Serwerowe Pssm Official:",0}}
setElementData(scianatext,"sciana:text",textView)


addEvent("admin:addText", true)
addEventHandler("admin:addText", root, function(text,id)
	table.insert(textView, {text,id})
	if #textView > 18 then
		table.remove(textView, 2)
	end
	setElementData(scianatext,"sciana:text",textView)
end)

addEvent("admin:removeText", true)
addEventHandler("admin:removeText", root, function(id)
	for i=#textView, 2, -1 do
		if textView[i][2] == id then
			table.remove(textView,i)
		end
	end
	setElementData(scianatext,"sciana:text",textView)
end)


addCommandHandler("report", function(plr,cmd,cel,...)
	if not cel then
		outputChatBox("#ff0000[Report]#FFFFFF Użycie: /report <nick/ID> <powód>", plr,255,255,255,true)
		return
	end
	local target=exports["ogrpg-core"]:findPlayer(plr,cel)
	if not target then
		outputChatBox("#ff0000[Report]#FF0000 Nie znaleziono podanego gracza.", plr, 255, 0, 0,true)
		return
	end
		if getElementData(plr,"blokadareport") then return outputChatBox("#ff0000[Report]#ffffff Możesz wysłać tylko jednego reporta na minutę.", plr, 255, 0, 0,true) end
		setElementData(plr,"blokadareport",true)
		if getElementData(plr,"blokadareport") then
setTimer(function()
		for _,p in pairs(getElementsByType('player'),root,false) do
			if getElementData(p,"blokadareport") then
				if getElementData(p,"player:uid") == getElementData(plr,"player:uid") then
				setElementData(p,"blokadareport",false)
				end
			end
		end
end, 60000, 1)
end
	local level = tonumber(getElementData(plr, "player:level"))
	if level == 1 then
	prgb = "#00CC00["..getElementData(plr,"id").."]"
	elseif level == 2 then
	prgb = "#006699["..getElementData(plr,"id").."]"
	elseif level == 3 then
	prgb = "#3333FF["..getElementData(plr,"id").."]"
	elseif level == 4 then
	prgb = "#FF3333["..getElementData(plr,"id").."]"
	elseif level == 5 then
	prgb = "#FF3333["..getElementData(plr,"id").."]"
	elseif getElementData(plr,"player:premium") then
	prgb ="#dbb234["..getElementData(plr,"id").."]"
	else
	prgb ="#ffffff["..getElementData(plr,"id").."]"
	end
	local levelt = tonumber(getElementData(target, "player:level"))
	if levelt == 1 then
	trgb = "#00CC00["..getElementData(target,"id").."]"
	elseif levelt == 2 then
	trgb = "#006699["..getElementData(target,"id").."]"
	elseif levelt == 3 then
	trgb = "#3333FF["..getElementData(target,"id").."]"
	elseif levelt == 4 then
	trgb = "#FF3333["..getElementData(target,"id").."]"
	elseif levelt == 5 then
	trgb = "#FF3333["..getElementData(target,"id").."]"
	elseif getElementData(target,"player:premium") then
	trgb ="#dbb234["..getElementData(target,"id").."]"
	else
	trgb ="#ffffff["..getElementData(target,"id").."]"
	end
	local text=table.concat({...}, " ")
	desc="#808080"..getPlayerName(plr):gsub("#%x%x%x%x%x%x","")..""..prgb.."#ffffff >> #808080"..getPlayerName(target):gsub("#%x%x%x%x%x%x","")..""..trgb..":#ffffff "..text:gsub("#%x%x%x%x%x%x","")
	triggerEvent("admin:addReport", resourceRoot, desc, getElementData(target,"id"))
	setElementData(plr,"player:report",getElementData(target,"id"),true)
	outputChatBox("#ff0000[Report] #808080Pomyślnie wysłano zgłoszenie na gracza #ff0000"..getPlayerName(target):gsub("#%x%x%x%x%x%x",""), plr,255,255,255,true)
end)

addCommandHandler("cl", function(plr,cmd,id,powod,...)
	if not getAdmin(plr) then
		return
	end
	local reason=table.concat({...}, " ")
		if not powod then
			outputChatBox("#BC8F8F[DUTY]#FF0000 Podaj powód usunięcia zgłoszenia.", plr, 255, 0, 0,true)
			return
	end
	local target=exports["ogrpg-core"]:findPlayer(plr,id)
	opis = "Brak Online"
	id=tonumber(id)
	if target then
		id=getElementData(target,"id")
		opis=getPlayerName(target)
		if target ~= plr and not getAdmin(target) then
		end
		exports["ogrpg-db"]:dbSet("INSERT INTO pssm_duty_logs (nick,operation) VALUES (?,?)", ""..getPlayerName(plr):gsub("#%x%x%x%x%x%x","").." [UID:"..getElementData(plr,"player:uid").."]", "Przyjęcie /raport od gracza "..opis:gsub("#%x%x%x%x%x%x","").."")
	end
	for i,v in ipairs(getElementsByType("player")) do
		if getElementData(v,"player:report") == id then
		if getElementData(plr,"player:level") == 1 then adminrank="#00CC00Supporter" elseif getElementData(plr,"player:level") == 2 then adminrank="#006699Moderator" elseif getElementData(plr,"player:level") == 3 then adminrank="#3333FFAdministrator"  elseif getElementData(plr,"player:level") == 4 or getElementData(plr,"player:level") == 5  then adminrank="#FF3333RCON" end 
	    outputChatBox(""..adminrank.." #ff0000"..getPlayerName(plr):gsub("#%x%x%x%x%x%x","").." #ffffffsprawdza twoje zgłoszenie.",v,255,255,255,true)
		end
		 
		if getAdmin(v) then
				triggerEvent("admin:removeReport", resourceRoot, id)
				outputChatBox("#ff0000[Reporty] #ccc000"..getPlayerName(plr):gsub("#%x%x%x%x%x%x","").." #808080usunął/ęła raport na #ccc000"..opis:gsub("#%x%x%x%x%x%x","").."["..id.."]: "..powod, v, 255, 0, 0,true)
		--	end
		end
	end
end)